import pandas as pd
from pyecharts import options as opts
from pyecharts.charts import Line, Timeline
import datetime
# 数据预处理
data = pd.read_csv("../data/bili2.csv")
data.columns = ['date', 'close', 'open', 'high', 'low', 'volume', 'percentage']
data['date'] = pd.to_datetime(data.date, format="%Y年%m月%d日")
data = data.sort_values(by='date').reset_index(drop=True)

# 日期格式转换
min_date = data['date'].min().strftime('%Y-%m-%d')
max_date = data['date'].max().strftime('%Y-%m-%d')

# 可视化符号配置
symbols = {'TV':"path://M830.577778 227.555556H657.066667l74.903703-70.162963c11.377778-11.377778 11.377778-29.392593 0-39.822223-5.688889-5.688889-13.274074-8.533333-21.807407-8.533333-7.585185 0-15.17037 2.844444-21.807407 8.533333L570.785185 227.555556H456.059259L338.488889 117.57037c-5.688889-5.688889-13.274074-8.533333-21.807408-8.533333-7.585185 0-15.17037 2.844444-21.807407 8.533333-11.377778 11.377778-11.377778 29.392593 0 39.822223L369.777778 227.555556H193.422222C117.57037 227.555556 56.888889 295.822222 56.888889 381.155556v332.8c0 85.333333 60.681481 153.6 136.533333 153.6h42.666667c0 25.6 22.755556 47.407407 50.251852 47.407407s50.251852-20.859259 50.251852-47.407407h353.659259c0 25.6 22.755556 47.407407 50.251852 47.407407s50.251852-20.859259 50.251852-47.407407h38.874074c75.851852 0 136.533333-69.214815 136.533333-153.6V381.155556c0.948148-85.333333-59.733333-153.6-135.585185-153.6zM698.785185 574.577778L425.718519 733.866667c-22.755556 13.274074-41.718519 2.844444-41.718519-24.651852V389.688889c0-26.548148 18.962963-37.925926 41.718519-24.651852l273.066666 160.237037c22.755556 14.222222 22.755556 35.081481 0 49.303704z"}
color0 = ['#FF76A2']

# 构建时间轴图表
tl = Timeline()
for i in range(0, len(data)):
    coordy_close = list(data['close'])[i]
    coordx_close = list(data['date'])[i]
    x_max = list(data['date'])[i] + datetime.timedelta(days=70)
    y_max = int(max(list(data['close'])[0:i + 1])) + 3
    title_date = list(data['date'])[i].strftime('%Y-%m-%d')
    c = (
        Line(
            init_opts=opts.InitOpts(
                width='1500px',
                height='900px', )
        )
        .add_xaxis(list(data['date'])[0:i])
        .add_yaxis(
            series_name="",
            y_axis=list(data['close'])[0:i], is_smooth=True, is_symbol_show=False,
            linestyle_opts=opts.LineStyleOpts(color=color0[0], width=6),
            markpoint_opts=opts.MarkPointOpts(
                data=[
                    opts.MarkPointItem(name="close_price", value=coordy_close, coord=[coordx_close, coordy_close],
                                       symbol=symbols['TV'], symbol_size=30,  # symbol=symbols['circle'],symbol_size=20,
                                       itemstyle_opts=opts.ItemStyleOpts(color=color0[0])),  # ,border_color="red"
                ],
                label_opts=opts.LabelOpts(font_size=18, color=color0[0], font_weight="bold", position="right")
            )

        )

        .set_global_opts(
            opts.TitleOpts(title="bilibili上市以来每日收盘价\n\n", subtitle="{}".format(title_date),pos_left=350,
                                      padding=[30, 20]),
            xaxis_opts=opts.AxisOpts(type_="time", max_=x_max),
            yaxis_opts=opts.AxisOpts(min_=9, max_=y_max),
        )
    )
    tl.add(c, "{}".format(list(data['date'])[i]))
    tl.add_schema(axis_type='time', play_interval=30, pos_bottom="-29px",
                  is_loop_play=False, width="780px",
                  pos_left='30px')  # ,pos_top=470,label_opts = opts.LabelOpts(is_show=False)
# # 时间轴配置

# 生成基础HTML
tl.render("../单线-有图标.html")


# 注入自定义HTML
with open("../单线-有图标.html", "r+", encoding="utf-8") as f:
    content = f.read()
    f.seek(0)
    f.write(content)
    f.truncate()